package com.jala.user;

import com.jala.base.BaseAction;
import com.jala.cm.Paginator;
import com.jala.user.api.UserApiService;
import com.jala.user.model.UserVO;
import com.jala.user.req.PageUserReq;
import com.jala.user.resp.PageUserResp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/13.
 */
@Controller
@RequestMapping("/user")
public class UserAction extends BaseAction{

    @Autowired
    private UserApiService userApiService;

    @RequestMapping("/pageUserList.htm")
    public String pageUserList(ModelMap model, Paginator page, @ModelAttribute UserVO userVO) {
        logger.info("********pageUserList.htm**********");

        //获取传入值
        PageUserReq req = new PageUserReq();
        if (StringUtils.isNotEmpty(userVO.getLoginId())) {
            userVO.setLoginId(userVO.getLoginId().trim());
        }
        if (StringUtils.isNotEmpty(userVO.getUserName())) {
            userVO.setUserName(userVO.getUserName().trim());
        }
        req.setLoginId(userVO.getLoginId());
        req.setUserName(userVO.getUserName());
        req.setPageNo(page.getPageNum());
        req.setPageSize(page.getPageSize());
        PageUserResp resp = userApiService.pageUserList(req);
        List<UserVO> userVOS = resp.getUserList();
        page.setTotalCount(resp.getTotalCount());
        model.put("paginator", page);
        model.put("userVOS", userVOS);
        model.put("userVO", userVO);
        return "user/list";
    }

    @RequestMapping("/addUser.htm")
    public String addUser(ModelMap model, Paginator page, @ModelAttribute UserVO userVO) {
        PageUserReq req = new PageUserReq();
        req.setLoginId(userVO.getLoginId());
        req.setUserName(userVO.getUserName());
        req.setPageNo(page.getPageNum());
        req.setPageSize(page.getPageSize());
        PageUserResp resp = userApiService.pageUserList(req);
        List<UserVO> userVOS = resp.getUserList();
        page.setTotalCount(resp.getTotalCount());
        model.put("paginator", page);
        model.put("userVOS", userVOS);
        model.put("userVO", userVO);
        return "user/add";
    }

    @RequestMapping("/chart.htm")
    public String chart() {
        return "user/chart";
    }

    @RequestMapping("/userDetail.htm")
    public String userDetail(ModelMap model, Paginator page, @ModelAttribute UserVO userVO) {
        PageUserReq req = new PageUserReq();
        req.setLoginId(userVO.getLoginId());
        req.setUserName(userVO.getUserName());
        req.setPageNo(page.getPageNum());
        req.setPageSize(page.getPageSize());
        PageUserResp resp = userApiService.pageUserList(req);
        List<UserVO> userVOS = resp.getUserList();
        page.setTotalCount(resp.getTotalCount());
        model.put("paginator", page);
        model.put("userVOS", userVOS);
        model.put("userVO", userVO);

        List<MenuVO> list = new ArrayList<>();
        MenuVO mv = new MenuVO();
        mv.setMenuId(1);
        mv.setParentMenuId(0);
        mv.setMenuName("水果");

        MenuVO mv1 = new MenuVO();
        mv1.setMenuId(2);
        mv1.setParentMenuId(1);
        mv1.setMenuName("苹果");

        MenuVO mv2 = new MenuVO();
        mv2.setMenuId(3);
        mv2.setParentMenuId(1);
        mv2.setMenuName("香蕉");

        MenuVO mv3 = new MenuVO();
        mv3.setMenuId(4);
        mv3.setParentMenuId(3);
        mv3.setMenuName("栗子");

        MenuVO mv4 = new MenuVO();
        mv4.setMenuId(5);
        mv4.setParentMenuId(2);
        mv4.setMenuName("榴莲");
        list.add(mv);
        list.add(mv1);
        list.add(mv2);
        list.add(mv3);
        list.add(mv4);
        model.put("menus",list);
        return "user/detail";
    }

    @RequestMapping("/ajaxUserInfo.json")
    @ResponseBody
    public List<MenuVO> ajaxUserInfo(ModelMap model, Paginator page, @ModelAttribute UserVO userVO) {
        List<MenuVO> list = new ArrayList<>();
        MenuVO mv = new MenuVO();
        mv.setMenuId(1);
        mv.setParentMenuId(0);
        mv.setMenuName("水果");

        MenuVO mv1 = new MenuVO();
        mv1.setMenuId(2);
        mv1.setParentMenuId(1);
        mv1.setMenuName("苹果");

        MenuVO mv2 = new MenuVO();
        mv2.setMenuId(3);
        mv2.setParentMenuId(1);
        mv2.setMenuName("香蕉");

        MenuVO mv3 = new MenuVO();
        mv3.setMenuId(4);
        mv3.setParentMenuId(3);
        mv3.setMenuName("栗子");

        MenuVO mv4 = new MenuVO();
        mv4.setMenuId(5);
        mv4.setParentMenuId(2);
        mv4.setMenuName("榴莲");
        list.add(mv);
        list.add(mv1);
        list.add(mv2);
        list.add(mv3);
        list.add(mv4);
        return list;
    }
}
